package model

import (
	"time"
)

const TracingStatusDisabled = -1
const TracingStatusEnabled = StatusNormal

const TracingTypeTrigger = 1
const TracingTypeTimer = 2

type ITracingIdContainer interface {
	GetTracingIds() []int64
}

type MapTracing map[int64]*Tracing

type Tracing struct {
	Id          int64     `xorm:"pk autoincr" form:"-"`
	UserId      int64     `form:"-"`
	ProjectId   int64     `form:"-"`
	Key         string    
	Status      int       
	Title       string    `valid:"Required;MaxSize(50);"`
	Type        int       `valid:"Required"`
	TracingTime string    `valid:"MaxSize(50)"`
	CreateTime  time.Time `xorm:"created" form:"-"`
}

func (mt MapTracing) GetTracingIds() []int64 {
	tracingIds := []int64{}
	for _, v := range mt {
		tracingIds = append(tracingIds, v.Id)
	}

	return tracingIds
}

func (mt MapTracing) GetUserIds() []int64 {

	userIds := []int64{}
	for _, v := range mt {
		userIds = append(userIds, v.UserId)
	}

	return userIds
}

func (mt MapTracing) GetProjectIds() []int64 {
	projectIds := []int64{}
	for _, v := range mt {
		projectIds = append(projectIds, v.ProjectId)
	}

	return projectIds
}

func NewTracing() *Tracing {
	t := new(Tracing)
	t.Status = StatusNormal
	return t
}

func GetTracingFromTracingIdContainer(tracingIdContainer ITracingIdContainer) (MapTracing, error) {
	tracingIds := tracingIdContainer.GetTracingIds()

	tracings := make(MapTracing)
	//保证空的也能返回东西
	if len(tracingIds) <= 0 {
		return tracings, nil
	}

	err := Orm.In("id", tracingIds).Find(&tracings)
	if err != nil {
		return nil, err
	}

	return tracings, nil
}
